<template>
  <transition name="dialog-fade">
    <!-- 遮罩层 -->
    <div class="dialog-wrapper" @click.self="sendCloseMsg" v-show="visible">
      <!-- 对话框 -->
      <div class="dialog" :style="{width, top, left}">
        <!-- 对话框头部 -->
        <div class="dialog-header">
          <!-- 这种写法可以让用户不使用 span，可以使用其他标签，给定一个具名插槽即可 -->
          <slot name="title">
            <!-- 当没有使用插槽时，默认渲染 span，span 不传 title 也有默认值 -->
            <span>{{ title }}</span>
          </slot>
          <button class="header-btn" @click="sendCloseMsg">
            <i class="icon-close"></i>
          </button>
        </div>
        <!-- 对话框中间 -->
        <div class="dialog-body">
          <slot></slot>
        </div>
        <!-- 对话框底部 -->
        <div class="dialog-footer" v-if="$slots.footer">
          <slot name="footer">
          </slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: 's-dialog',
  props: {
    title: {
      type: String,
      default: '提示'
    },
    width: {
      type: String,
      default: ''
    },
    top: {
      type: String,
      default: ''
    },
    left: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      defalut: false
    }
  },
  methods: {
    sendCloseMsg () {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-wrapper {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;
  margin: 0;
  z-index: 1000;
  background-color: rgba(0, 0 ,0, .5);
  .dialog {
    position: relative;
    margin: 15vh auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0, 0 ,0, .3);
    box-sizing: border-box;
    width: 30%;
    z-index: 1001;
    .dialog-header {
      padding: 12px 20px 10px;
      span {
        line-height: 20px;
        font-size: 16px;
        color: #303133;
      }
      .header-btn {
        position: absolute;
        top: 12px;
        right: 15px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        i {
          color: 909399;
        }
      }
    }
  }
  .dialog-body {
    padding: 30px 20px;
    color: #606266;
    font-size: 14px;
    word-break: break-all;
  }
  .dialog-footer {
    padding: 10px 20px 20px;
    text-align: right;
    box-sizing: border-box;
    &:first-child {
      margin-right: 20px;
    }
  }
}

.dialog-fade-enter-active {
  animation: fade .3s;
}

.dialog-fade-leave-active {
  animation: fade .3s reverse;
}

@keyframes fade {
  0% {
    opacity: 0;
    transform: translateY(-20px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
